/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.bean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.netbrain.dao.OrganizationDao;
import mx.netbrain.dao.impl.OrganizationDaoImpl;
import mx.netbrain.model.Organization;
import mx.netbrain.model.Status;

/**
 *
 * @author gamma
 */
@ManagedBean(name = "organizationBean")
@SessionScoped
public class OrganizationBean implements Serializable{
    private Organization organization = new Organization();
    private Status status = new Status();
    private String nameOrg;
    private String descOrg;
    private List<Organization> listOrganization;
    
    /**
     * Creates a new OrganizationBean's instance
     */
    public OrganizationBean(){
    }
    
    public void prepareOrg(Long id){
        OrganizationDao organizationDao = new OrganizationDaoImpl();
        organization = organizationDao.findById(id);
    }
    
    public void saveOrg(){
        OrganizationDao organizationDao = new OrganizationDaoImpl();
        organization.setName(nameOrg);
        organization.setDescription(descOrg);
        status.setId(1);
        organization.setStatus(status);
        organization.setRegistro(new Date());
        organizationDao.save(organization);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("¡El registro se guardó correctamente!", "Guardado"));
        organization = new Organization();
        nameOrg = new String();
        descOrg = new String();
    }
    
    public void deleteOrg(){
        OrganizationDao organizationDao = new OrganizationDaoImpl();
        organizationDao.delete(organization);
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,"¡Se ha borrado el registro correctamente!", "Actualizado");
        FacesContext.getCurrentInstance().addMessage(null, message);
        organization = new Organization();
    }
    
    public void editOrg(){
        OrganizationDao organizationDao = new OrganizationDaoImpl();
        organizationDao.update(organization);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("¡Registro actualizado correctamente!", "Actualizado"));
        organization = new Organization();
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public String getNameOrg() {
        return nameOrg;
    }

    public void setNameOrg(String nameOrg) {
        this.nameOrg = nameOrg;
    }

    public String getDescOrg() {
        return descOrg;
    }

    public void setDescOrg(String descOrg) {
        this.descOrg = descOrg;
    }

    public List<Organization> getListOrganization() {
        OrganizationDao organizationDao = new OrganizationDaoImpl();
        listOrganization = organizationDao.listAll();
        return listOrganization;
    }

    public void setListOrganization(List<Organization> listOrganization) {
        this.listOrganization = listOrganization;
    }
}
